package com.crud.project.controller;

import com.crud.project.po.LoginUser;
import com.crud.project.service.LoginService;
import com.crud.project.util.MD5Utils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
@Controller
public class LoginController {
    @Resource
    private LoginService loginService;

    @GetMapping("/login")
    public String Login(){
        return "login";
    }
    @GetMapping("/loginChecked")
    public String LoginChecked(@RequestParam("username") String username, @RequestParam("password") String password){
        Subject subject= SecurityUtils.getSubject();
        UsernamePasswordToken token=new UsernamePasswordToken(username, password);

        try {
            subject.login(token);
            if (subject.isAuthenticated()==true) {
                System.out.println(subject.toString());
                System.out.println("成功");
            }
        } catch (AuthenticationException e) {
            e.printStackTrace();
            System.out.println(e);
        }

        return "redirect:/main";
    }

//    @GetMapping("/login")
//    public String Login(){
//        return "login";
//    }
//    @GetMapping("/loginChecked")
//    public String LoginChecked(@RequestParam String username, @RequestParam String password, HttpSession httpSession,Model model){
//        LoginUser loginUser=new LoginUser();
//        loginUser.setUsername(username);
//        loginUser.setPassword(MD5Utils.code(password));
//        LoginUser loginUser1=loginService.checkedLogin(loginUser);
//       if (loginUser1==null){
//           return "redirect:/login";
//       }
//       httpSession.setAttribute("loginUser1",loginUser1);
//       return "redirect:/main";
//    }
}

